import Fly from 'flyio'
const fly = new Fly()

fly.config.baseURL = 'https://toutiao.itheima.net/v1_0'
fly.config.timeout = 5000

//添加请求拦截器
fly.interceptors.request.use((request) => {
  //给所有请求添加自定义header
  const {token} = wx.getStorageSync('my_token')
  request.headers["X-Tag"] = "flyio";
  request.headers["Authorization"] = `Bearer ${token}`
  //打印出请求体
  // console.log(request.body)
  return request;
})

//添加响应拦截器，响应拦截器会在then/catch处理之前执行
fly.interceptors.response.use(
  (response) => {
    //只将请求结果的data字段返回
    return response.data
  },
  (err) => {
    //发生网络错误后会走到这里
    // console.dir(err);
    if(err.status === 401) {
      return Promise.resolve("token超时或者未传token")
    }
    //return Promise.resolve("ssss")
  }
)

export default fly